package cn.com.hhrcw.web;

import cn.com.hhrcw.entity.EnInfo;
import cn.com.hhrcw.entity.PiInfo;
import cn.com.hhrcw.entity.SysUserDepart;
import cn.com.hhrcw.service.IEnInfoService;
import cn.com.hhrcw.service.IFileService;
import cn.com.hhrcw.service.IPiInfoService;
import cn.com.hhrcw.service.ISysUserDepartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: recruit-boot-parent
 *
 * @author: JD
 * @create: 2020-11-24 14:42
 **/
@RestController
@RequestMapping("/sys")
@Slf4j
public class UserInfoController {

    @Autowired
    IPiInfoService piInfoService;

    @Autowired
    IFileService fileService;

    @Autowired
    ISysUserDepartService departService;

    @Autowired
    private IEnInfoService enInfoService;

    @Autowired
    private ISysUserDepartService sysUserDepartService;

    @GetMapping("/getUserInfo")
    public Result<LoginUser> getUserInfo() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        PiInfo info = piInfoService.lambdaQuery().eq(PiInfo::getUserId, loginUser.getId()).eq(PiInfo::getDelFlag, false).one();

        LoginUser sysUser = new LoginUser();
        sysUser.setUsername(loginUser.getUsername());
        sysUser.setActivitiSync(loginUser.getActivitiSync());
        sysUser.setUserIdentity(loginUser.getUserIdentity());
        sysUser.setAvatar(loginUser.getAvatar());
        sysUser.setTelephone(loginUser.getTelephone());
        sysUser.setBirthday(loginUser.getBirthday());
        sysUser.setCreateTime(loginUser.getCreateTime());
        sysUser.setDelFlag(loginUser.getDelFlag());
        sysUser.setDepartIds(loginUser.getDepartIds());
        sysUser.setEmail(loginUser.getEmail());
        sysUser.setSex(loginUser.getSex());
        sysUser.setRealname(loginUser.getRealname());
        sysUser.setId(loginUser.getId());
        sysUser.setPost(loginUser.getPost());
        sysUser.setStatus(loginUser.getStatus());
        sysUser.setOrgCode(loginUser.getOrgCode());
        sysUser.setPhone(loginUser.getPhone());
        sysUser.setRelTenantIds(loginUser.getRelTenantIds());
        sysUser.setPhotoUrl(loginUser.getPhotoUrl());
        if (info != null) {
            sysUser.setEnRoleType(info.getEnRoleType());
        }
        List<SysUserDepart> count = departService.lambdaQuery().eq(SysUserDepart::getUserId, sysUser.getId()).list();
        if (CollectionUtils.isEmpty(count)) {
            sysUser.setReg(false);
        } else {
            String id = count.get(0).getDepId();
            EnInfo departInfo = enInfoService.lambdaQuery().eq(EnInfo::getId, id).eq(EnInfo::getDelFlag, false).one();
            if (departInfo == null) {
                sysUser.setReg(false);
            } else {
                sysUser.setEnState(departInfo.getState());
                EnInfoVO enInfoVO = new EnInfoVO(departInfo);
                List<SysUserDepart> departs = sysUserDepartService.lambdaQuery().eq(SysUserDepart::getDepId, departInfo.getId()).list();
                PiInfo piInfo = piInfoService.lambdaQuery().in(PiInfo::getUserId, departs.stream().map(SysUserDepart::getUserId).toArray()).eq(PiInfo::getEnRoleType, 1).eq(PiInfo::getDelFlag, false).one();
                enInfoVO.setPiInfo(piInfo);
                sysUser.setEnInfo(enInfoVO);
                sysUser.setReg(true);
            }
        }
        return Result.ok(sysUser);
    }
}
